package com.study.microservice.springcloud.service;

import com.study.microservice.springcloud.pojo.User;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Author : 周世雄 <br>
 * Date : 2018/11/12 15:29 <br>
 * E-mail : zhoushixiong@chinasoftinc.com
 * Description :
 */
@Component
public class FeignClientFallbackFactory implements FallbackFactory<UserFeignClient> {

    private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientFallbackFactory.class);

    /**
     * 在项目启动的时候就会执行该方法<br>
     * * 这里使用了lambda表达式，所以jdk版本需要在1.8及以上
     * @param cause
     * @return
     */
    @Override
    public UserFeignClient create(Throwable cause) {
        // 日志最好放在给个fallback中，而不要放在create方法中。否则会在启动的时候就打印该日志
        return id -> {
            FeignClientFallbackFactory.LOGGER.error("fallback; reason was:",cause);
            User user = new User();
            user.setId(-1L);
            user.setUsername("默认用户");
            return user;
        };
    }
}
